package com.hankers.localvideoplayer;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends ImmersiveActivity {

	static final int REQUEST_EXIT = 999;
	
	private ImageView _imgView;
	private VideoView _videoView;
	private FrameLayout _videoFrame;
	private ViewGroup _sidePanel;
	private TextView _txtQRCode;
	private ImageView _imgQRCode;
	private MyConfig _myconfig;
	private FolderRandomController _folderRandomPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	@Override
	protected void onStart() {
		super.onStart();

		_imgView = (ImageView) findViewById(R.id.imgView);
		_videoFrame = (FrameLayout) findViewById(R.id.videoFrame);
		_videoView = (VideoView) findViewById(R.id.videoView);
		_sidePanel = (ViewGroup) findViewById(R.id.side_panel_coupon);
		_txtQRCode = (TextView) findViewById(R.id.txtQRCode);
		_imgQRCode = (ImageView) findViewById(R.id.imgQRCode);
		
        setupExitDialog();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		_myconfig = new MyConfig(MainActivity.this);

		String absoluteDir = _myconfig.getProjectDir();
		File f = new File(absoluteDir);
		if(f.isDirectory()) {
			_folderRandomPlayer = new FolderRandomController(this, absoluteDir, _videoView);
		} else {
			Toast.makeText(this, R.string.directory_not_found + absoluteDir, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public void PlayOnUiThread(final String fileName, final String qrCode) {
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				playFile(fileName, qrCode);
			}
		});
	}

	@SuppressLint("DefaultLocale")
	private void playFile(final String filepath, final String qrCode) {
		String fname = filepath.toUpperCase();
		Log.i("MainActivity", "playing " + filepath);
		if (fname.endsWith(".JPG") || fname.endsWith(".PNG")
				|| fname.endsWith(".GIF")) {

			_imgView.setImageURI(Uri.parse(filepath));
			_imgView.setVisibility(View.VISIBLE);
			_videoView.stopPlayback();
			_videoView.setVisibility(View.GONE);
			_videoFrame.setVisibility(View.GONE);
		} else if (fname.endsWith(".MP4") || fname.endsWith(".AVI")
				|| fname.endsWith(".MPG") || fname.endsWith(".3GP")) {
			_imgView.setVisibility(View.GONE);
			_videoFrame.setVisibility(View.VISIBLE);
			_videoView.setVisibility(View.VISIBLE);
			_videoView.setVideoURI(Uri.parse(filepath));
			_videoView.start();
		} else {
		}
		
	}

	private void setupExitDialog() {
        _imgQRCode.setOnLongClickListener(new OnLongClickListener(){

			@Override
			public boolean onLongClick(View arg0) {
				
                Intent myIntent = new Intent(MainActivity.this, ExitActivity.class);
                MainActivity.this.startActivityForResult(myIntent, REQUEST_EXIT);

				return true;
			}
			
		});
	}
	
	// Dialog will show the navigation bar of system.
	private void popupExitDiaglog() {
		
//		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//		builder.setTitle("Exit");
//
//		// Set up the input
//		final EditText input = new EditText(MainActivity.this);
//		// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
//		input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//		builder.setView(input);
//
//		// Set up the buttons
//		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//		    @Override
//		    public void onClick(DialogInterface dialog, int which) {
//		        if(input.getText().toString().equals(new MyConfig(MainActivity.this).getExitPWD())) {
//		        	finish();
//		        }
//		    }
//		});
//		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//		    @Override
//		    public void onClick(DialogInterface dialog, int which) {
//		        dialog.cancel();
//		    }
//		});
//
//		builder.show();
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_EXIT) {
            if (resultCode == RESULT_OK) {
                String msg = data.getStringExtra("exit");
                if(msg.equals("1")) {
                    this.finish();
                }
            }
        }
    }
    
}
